<?php
namespace classes\customer;
use classes\bank\Bank;
use classes\bank\Deposit;
class Customer
{
	private $name;
	private $address;
	private $money;
	private $salary;
	private $deposits;
	private $credits;
	
	public function __construct($name, $address, $money, $salary)
	{
		echo "<p>Customer: $name</p>";
		$this->setName($name);
		$this->setAddress($address);
		$this->setMoney($money);
		$this->setSalary($salary);
		$this->deposits = array();
		$this->credits = array();
	}
	
	private function setName($name)
	{
		if(!ctype_alpha($name))
		{
			throw new \Exception("Name is not valid!");
		}
		$this->name = $name;
		return true;
	}
	
	public function getName()
	{
		return $this->name;
	}
	
	private function setAddress($address)
	{
		if(!is_string($address))
		{
			throw new \Exception("Address is not valid!");
		}
		if(is_null($address))
		{
			throw new \Exception("Address can not be empty!");
		}
		$this->address = $address;
		return true;
	}
	
	public function getAddress()
	{
		return $this->address;
	}
	
	public function setMoney($money)
	{
		if(!is_numeric($money))
		{
			throw new \Exception("Value for money is not valid!");
		}
		if(is_null($money))
		{
			throw new \Exception("Money can not be empty!");
		}
		$this->money = $money;
		return true;
	}
	
	public function getMoney()
	{
		return $this->money;
	}
	
	public function setSalary($salary)
	{
		if(!is_numeric($salary))
		{
			throw new \Exception("Invalid value for salary!");
		}
		if(is_null($salary))
		{
			throw new \Exception("Salary can not be empty!");
		}
		if($salary <= 0)
		{
			throw new \Exception("Salary can not be less than 0 !");
		}
		$this->salary = $salary;
		return true;
	}
	
	public function getSalary()
	{
		return $this->salary;
	}
	
	public function getCredits()
	{
		return $this->credits;
	}

	public function addCredit($credit)
	{
		$this->credits[] = $credit;
	}
	
	public function addDeposit($deposit)
	{
		$this->deposits[] = $deposit;
	}
	
	public function getDeposit()
	{
		return $this->deposits;
	}
	public function askCredit(Bank $bank, $amount, $period, $customer, $creditType)
	{
	
		$bank->giveCredit($amount, $period, $customer, $creditType);
	}
	public function makeDeposit(Bank $bank, Deposit $deposit, Customer $customer)
	{
		$bank->addDeposit($deposit, $customer);
		$this->deposits[] = $deposit;
	}
	
	public function showInfo(){
		
		echo "<b>Report for Customer: </b><br>" .
			 "Name: " . $this->name . "<br>" .
		     "Address: " . $this->address . "<br>" .
			 "Money in cash: "	. $this->money . "<br>" .
			 "Salary: " . $this->salary . "<br>";
	}
}